package configrations

import (
	"time"
)

type MysqlConfigration struct {
	ConfigDir       string
	FileName        string
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
	ConnMaxIdleTime time.Duration
	MaxRetries      int
	BaseDelay       time.Duration
	MaxDelay        time.Duration
	LogLevel        string
}

type MysqlConfigrationOption func(*MysqlConfigration)

func NewMysqlConfigration(filename string, configDir string, opts ...MysqlConfigrationOption) *MysqlConfigration {
	config := &MysqlConfigration{
		FileName:        filename,
		ConfigDir:       configDir,
		MaxOpenConns:    200,
		MaxIdleConns:    20,
		ConnMaxLifetime: 30 * time.Minute,
		ConnMaxIdleTime: 10 * time.Minute,
		MaxRetries:      7,
		BaseDelay:       1 * time.Second,
		MaxDelay:        30 * time.Second,
		LogLevel:        "info",
	}
	for _, opt := range opts {
		opt(config)
	}

	return config
}
func WithConfigDir(dir string) MysqlConfigrationOption {
	return func(c *MysqlConfigration) {
		c.ConfigDir = dir
	}
}

func WithMaxOpenConns(n int) MysqlConfigrationOption {
	return func(c *MysqlConfigration) {
		c.MaxOpenConns = n
	}
}

func WithMaxIdleConns(n int) MysqlConfigrationOption {
	return func(c *MysqlConfigration) {
		c.MaxIdleConns = n
	}
}

func WithConnMaxLifetime(d time.Duration) MysqlConfigrationOption {
	return func(c *MysqlConfigration) {
		c.ConnMaxLifetime = d
	}
}

func WithConnMaxIdleTime(d time.Duration) MysqlConfigrationOption {
	return func(c *MysqlConfigration) {
		c.ConnMaxIdleTime = d
	}
}

func WithMaxRetries(n int) MysqlConfigrationOption {
	return func(c *MysqlConfigration) {
		c.MaxRetries = n
	}
}

func WithBaseDelay(d time.Duration) MysqlConfigrationOption {
	return func(c *MysqlConfigration) {
		c.BaseDelay = d
	}
}

func WithMaxDelay(d time.Duration) MysqlConfigrationOption {
	return func(c *MysqlConfigration) {
		c.MaxDelay = d
	}
}

func WithLogLevel(level string) MysqlConfigrationOption {
	return func(c *MysqlConfigration) {
		c.LogLevel = level
	}
}
